package com.travel.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.travel.pojo.User;
import com.travel.service.impl.UserServiceImpl;
import com.travel.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.UUID;

@RestController
public class UserController {
    @Autowired
    private UserServiceImpl userService;
    @PostMapping("/login/list")
    public Result<User> getUser(@RequestBody User user){
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", user.getUsername()).eq("password", user.getPassword());
        User u = userService.getOne(queryWrapper);
        if (user != null) {
            return new Result<>(u,"登陆成功",200);
        }
        return new Result<>("用户名或密码错误",500);
    }
    @PostMapping("/login/register")
    public Result register(@RequestBody User user){
        QueryWrapper<User> wrapper = new QueryWrapper<User>().eq("username",user.getUsername());
        User user1 = userService.getOne(wrapper);
        Result<Object> result = new Result<>();
        if (user1 == null){
            int i = (int) Math.floor(0 + Math.random() * 9999);
            user.setId(i);
            boolean save = userService.save(user);
            result.setData(save);
            result.setCode(200);
        }else {
            result.setCode(400);
        }
        return result;
    }
}
